package shuati.mydata;

import java.sql.*;

public class MyDataBae {
    public static Connection connection;
    public static void main(String[] args) {
        /*
        １.jdbc数据库驱动
            普通项目：lib
                把下载的驱动　jar包放到lib目录
            maven项目：pom.xml

           2. 加载驱动
           3. 连接
         */

        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("数据库驱动成功加载");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败");
            throw new RuntimeException(e);
        }



        String url = "jdbc:mysql://localhost:3306/login?useSSL=false";
        String user = "root";
        String password = "8866";
        try {
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功");
        } catch (SQLException e) {
            System.out.println("数据库连接失败！");
            throw new RuntimeException(e);
        }

        /*
        增删改查
         */

//        //
//        boolean insert = insertRecord("lisig", "vbjkgsdvbkjb");
//        if (insert){
//            System.out.println("数据插入成功");
//        } else {
//            System.out.println("数据插入失败");
//        }


//        deleteRecord(3);
        updateRecord(2,"zhang");
        selectRecords();

    }

    // 增加记录
    public static boolean insertRecord(String name, String password) {
        String sql = "INSERT INTO user (uname, password) VALUES (?, ?)";
        try  {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, password);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    // 删除记录
    public static boolean deleteRecord(int id) {
        String sql = "DELETE FROM user WHERE uid=?";
        try  {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新记录
    public static boolean updateRecord(int id, String newName) {
        String sql = "UPDATE user SET uname=? WHERE uid=?";
        try  {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, newName);
            pstmt.setInt(2, id);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 查询记录
    public static void selectRecords() {
        String sql = "SELECT uid, uname, password FROM user";
        try (
             Statement stmt = connection.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                int id = rs.getInt("uid");
                String name = rs.getString("uname");
                String password = rs.getString("password");
                System.out.println("ID: " + id + ", Name: " + name + ", password: " + password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
